/*
Miles: a set of useful utilities and base classes
Copyright (C) 2004  Jeffrey J. Melby

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package com.dreamlizard.miles.beans;


import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;

/**
 * Property editor for Timestamps.
 */
public class TimestampPropertyEditor extends PropertyEditorSupport
{
    /**
     * Gets the property value as a string suitable for presentation
     * to a human to edit.
     *
     * @return The property value as a string suitable for presentation
     *         to a human to edit.
     *         <p>   Returns "null" is the value can't be expressed as a string.
     *         <p>   If a non-null value is returned, then the PropertyEditor should
     *         be prepared to parse that string back in setAsText().
     */
    @Override
    public String getAsText()
    {
        String text = null;
        if (getValue() != null)
        {
            text = getValue().toString();
        }
        return text;
    }

    /**
     * Sets the property value by parsing a given String.  May raise
     * java.lang.IllegalArgumentException if either the String is
     * badly formatted or if this kind of property can't be expressed
     * as text.
     *
     * @param text The string to be parsed.
     */
    @Override
    public void setAsText(String text) throws IllegalArgumentException
    {
        // cao: also check if empty to allow for timestamp fields that are optional
        if ((text != null) && (!text.isEmpty()))
        {
            setValue(Timestamp.valueOf(text));
        }
        else
        {
            setValue(null);
        }
    }
}